#!/usr/bin/env perl

# Read command line parameters to set memory and CPU limits.
my $maxmem="2048m";

my $allArgs = join(" ",@ARGV);

if ($allArgs =~ /-h/)
{
	usage();
    exit(0);
}

if ($allArgs =~ /-m\s*(\d+)m?/)
{
	$maxmem = $1."m";
    print "\nusing memory $maxmem\n\n";
}


my $uname = `uname`;

my $platform = "";
my $word;
if ($allArgs =~ /-linux/i)
{
	$platform = "linux";
    $word = getWordSize();
}
elsif ($allArgs =~ /-mac/i)
{
	$platform = "mac";
}
elsif ($uname =~ /darwin/i)
{
	$platform = "mac";
    $word = "64"; # setting not used
}
elsif ($uname =~ /linux/i)
{
	$platform = "linux";
    $word = getWordSize();
}
else
{
	print "Unable to determine platform; call with either '-linux' or '-mac'\n";
    exit(0); 	
}

my $libpath="";
if ($platform eq "linux")
{
	if ($word eq "64")
    {
   		$libpath="-Djava.library.path=java/lib/amd64/";
  	}
    else
    {
		$libpath="-Djava.library.path=java/lib/i386/";
   	}
}
else 
{
	$libpath=" -Djava.library.path=java/lib/osx -Dj3d.reng=jogl ";
}
my $no3D = ($allArgs =~ /-no3d/i);
my $jar = ($no3D ? "symap.jar" : "symap3D.jar");
my $main = ($no3D  ? "symapCE.ProjectManagerFrame" : "symap3D.ProjectManagerFrame3D");
my $cmd = "java -Xmx$maxmem $libpath ";
$cmd .= " -classpath java/jar/$jar:java/jar/mysql-connector-java-5.0.8-bin.jar:java/jar/mysql-connector-mxj-gpl-5-0-9.jar:java/jar/mysql-connector-mxj-gpl-5-0-9-db-files.jar:java/jar/biojava.jar:java/jar/aspectjrt.jar:java/jar/freehep-graphicsio-2.1.1.jar:java/jar/freehep-graphicsio-svg-2.1.1.jar:java/jar/freehep-graphics2d-2.1.1.jar:java/jar/freehep-util-2.0.2.jar:java/jar/freehep-io-2.0.2.jar:java/jar/freehep-xml-2.1.1.jar:java/jar/freehep-export-2.1.1.jar:java/jar/freehep-swing-2.0.3.jar:java/jar/freehep-graphicsio-ps-2.1.1.jar:java/jar/freehep-graphicsio-pdf-2.1.1.jar";
if (not $no3D)
{
	$cmd .= ":java/jar/gluegen-rt.jar:java/jar/j3dcore.jar:java/jar/j3dutils.jar:java/jar/jogl.jar:java/jar/vecmath.jar";
}

# Get rid of args not used by app, and format the -p with no space
$allArgs =~ s/-32//;
$allArgs =~ s/-64//;
$allArgs =~ s/-m\S+//i;
$allArgs =~ s/-mac//i;
$allArgs =~ s/-linux//i;
$allArgs =~ s/-no3d//i;
if ($allArgs =~ /-p\s+\d+/)
{
	$allArgs =~ s/(-p)\s+(\d+)/$1$2/;
}
$cmd .= " $main $allArgs";
#print "\n$cmd\n";
system($cmd);


#############################################

# note two tabs after each option
sub usage
{
	print <<END;

	Usage:  ./symap [options]
    
	-h		: print usage
	-r		: read-only mode
	-32		: 32-bit mode
	-64		: 64-bit mode
	-mN		: set max memory to N megabytes
	-no3d		: don't show the 3D display 
	-linux 		: linux platform
	-mac		: mac platform
    
END

}

#############################################

# should only be called for platform linux
sub getWordSize
{
	if ($allArgs =~ /-32/)
    {
    	return "32";
    }
    elsif ($allArgs =~ /-64/)
    {
    	return "64";
    }
    my $uname = `uname -i`; # illegal option on mac
    if ($uname =~ /i386/)
    {
    	return "32";
    }
    return "64";
    
}
